//
//  HJVoiceRightCell.m
//  project
//
//  Created by hu on 2019/3/28.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJVoiceRightCell.h"
#import "HJVocie.h"
#import <AVFoundation/AVFoundation.h>
#import "HJVociePlayer.h"

@interface HJVoiceRightCell ()<AVAudioPlayerDelegate>
/*
 * 头像父视图
 */
@property (weak, nonatomic) IBOutlet UIView *headSubView;
/*
 * 头像图片
 */
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
/*
 * 语音按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *vocieBtn;
/*
 * 语音秒数
 */
@property (weak, nonatomic) IBOutlet UILabel *vocieNum;
/*
 * 语音按钮宽度
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *vocieBtn_H;

@property (weak, nonatomic) IBOutlet UIButton *sendError;
@property (nonatomic,strong) RCMessage * vocieMsg;
@property (nonatomic,strong) AVAudioPlayer *Player;
@property (nonatomic,strong) HJGifImageView * gifImage;
@property (weak, nonatomic) IBOutlet UIImageView *yuyinImage;

@end


@implementation HJVoiceRightCell

-(void)initGifImage {
    NSString * path = [[NSBundle mainBundle] pathForResource:@"语音播放 白色" ofType:@"gif"];
    _gifImage = [[HJGifImageView alloc] initWithGIFPath:path];
    _gifImage.frame = CGRectMake(self.yuyinImage.left, self.yuyinImage.top, self.yuyinImage.width, self.yuyinImage.height);
    [self.contentView addSubview:_gifImage];
    [self.contentView bringSubviewToFront:_gifImage];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // 用户图片 圆角
    [self.headImage rounded:(UIRectCornerAllCorners) radius:20];
    // 图片外的圆环
    [self.headSubView rounded:(UIRectCornerAllCorners) radius:20 borderColor:[UIColor blackColor] borderWidth:1];
    self.vocieBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self initGifImage];
}

- (IBAction)vocieBtnClicked:(id)sender {
    RCVoiceMessage * vocieMsg = (RCVoiceMessage *)self.vocieMsg.content;
    if (_delegate && [_delegate respondsToSelector:@selector(voiceMessageTaped:data:isRgiht:)]) {
        [_delegate voiceMessageTaped:self data:vocieMsg.wavAudioData isRgiht:YES];
    }
}

- (void)setVoicePlayState:(LGVoicePlayState)voicePlayState {
    if (_voicePlayState != voicePlayState) {
        _voicePlayState = voicePlayState;
    }
    
    if (_voicePlayState == LGVoicePlayStatePlaying) {
        self.yuyinImage.image = [UIImage new];
        [self.gifImage setHidden:NO];
        [self.gifImage startGIF];
    }else if (_voicePlayState == LGVoicePlayStateDownloading) {
        [self.gifImage setHidden:YES];
        [self.gifImage stopGIF];
        self.yuyinImage.image = setImage(@"yuyin_chat_white");
    }else {
        self.yuyinImage.image = setImage(@"yuyin_chat_white");
        [self.gifImage setHidden:YES];
        [self.gifImage stopGIF];
    }
}


// 发送失败
- (IBAction)sendErrorBtnClicked:(UIButton *)sender {
    HJUserModel *userModel = [HJUserManager getUserModel];
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * okAlert = [UIAlertAction actionWithTitle:@"重新发送" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        RCVoiceMessage * vocieMsg = (RCVoiceMessage *)self.vocieMsg.content;
        RCVoiceMessage *RCvocieMsg = [RCVoiceMessage messageWithAudio:vocieMsg.wavAudioData duration:vocieMsg.duration];
        RCvocieMsg.senderUserInfo = [[RCUserInfo alloc]initWithUserId:userModel.userId name:userModel.nickName portrait:userModel.avatar];
        [[RCIMClient sharedRCIMClient] sendMessage:ConversationType_PRIVATE targetId:self.targetId content:RCvocieMsg pushContent:@"语音" pushData:nil success:^(long messageId) {
            NSLog(@"发送语音消息成功");
        } error:^(RCErrorCode nErrorCode, long messageId) {
            NSLog(@"发送语音消息失败，错误码是(%ld)", (long)nErrorCode);
        }];
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:okAlert];
    [alert addAction:cancel];
    [[UIViewController currentViewController] presentViewController:alert animated:YES completion:nil];
    
}

-(void)loadRightVocieMsg:(RCMessage *)msg {
    self.vocieMsg = msg;
    HJUserModel *userModel = [HJUserManager getUserModel];
    if (msg.sentStatus == SentStatus_FAILED) {
        self.sendError.hidden = NO;
    }else {
        self.sendError.hidden = YES;
    }
    RCVoiceMessage * vocieMsg = (RCVoiceMessage *)msg.content;
    CGFloat width =SCREEN_WIDTH-85-100;
    if ([self vocieBtnWidthWithDuration:vocieMsg.duration] >= width) {
         self.vocieBtn_H.constant = width;
    }else {
        self.vocieBtn_H.constant = [self vocieBtnWidthWithDuration:vocieMsg.duration];
    }
    self.vocieNum.text = [NSString stringWithFormat:@"%ld''",vocieMsg.duration];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:userModel.avatar] placeholderImage:setImage(@"img_mydongtai_defualt_avatar")];
}
-(CGFloat)vocieBtnWidthWithDuration:(long)seconds {
    CGFloat maxWidth = SCREEN_WIDTH-85-100;
    return (CGFloat)(maxWidth / 60 * seconds) +80;
}



@end
